<?php
/**
 * 
 * @author tangbl
 * 
 */
class GroupouAction extends CommonAction {
	
	public function _initialize() {
		parent::_initialize();
		parent::_initmenu();	
	}
		
	/**
	 * 列表
	 */
	public function index() {
		
		if(IS_AJAX){
			
			import('ORG.Util.Page');
			$model     = D("GroupouView");
			
			$orderDesc = $this->_get("orderDirection","trim","desc");;
			$orderName = $this->_get("orderName","trim","sort");
			$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
			$nowPage   = $this->_get("p","intval",1);
			
			$map       = unescape($this->_get("where"));
			$map       = "type=2 ".$map;
			$count     = $model->where($map)->count();
			$Page      = new Page($count);
			$show      = $Page->show();
			$list      = $model->where($map)->order($orderName.' '.$orderDesc)
						 ->page($nowPage.','.$pageNum)->select();
			$totalPage = ceil($count/$pageNum);
			C('web_admin_pagenum',$pageNum);
			
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
			$upd       = L('cfg_list_itf_btn_modify');
			$del       = L('cfg_list_itf_btn_delete');
			foreach($list AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'        => $row['id'],
						'lang'      => $row['lang'],
						'begintime' => date("Y-m-d H:i:s",$row['begintime']),
						'endtime'   => date("Y-m-d H:i:s",$row['endtime']),
						'title'     => $row['title'],
						'mprice'    => $row['mprice'],
						'sprice'    => $row['sprice'],
						'sort'      => $row['sort'],
						'deal'=>'
					  <a href="'.__URL__.'/edit/id/'.$row['id'].'" class="btn blue" title="'.$upd.'"><i class="icon-edit"></i></a>
					  <a href="#" class="btn red icn-only delAlone" title="'.$del.'" data-id="'.$row['id'].'" pid="'.$row['pid'].'"><i class="icon-remove"></i></a> '
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,'JSON');
			
		}else{
			$this->display();
		}		
	}
	
	/**
	 * 删除
	 */
	public function del(){
				
		$param = array(
				"cache" => true,
		);
		$pid = $this->_param("pid","intval",0);
		if($pid){
			
			$where['pid'] = $pid;
			$rows =M("Groupou")->where($where)->find();
			
			$map  = array("id"=>$pid,"type"=>1);
			M("Product")->save($map);
			M("Product")->where("id=".$pid)->setInc("buynum",$rows['buynum']);

			parent::do_del("Groupou",true,$param);
		}else{
			$this->error();
		}
	}
	
	/**
	 * 修改
	 */
	public function edit(){
		
		if(IS_POST) {
			
			$btime = strtotime($this->_post("begintime"));
			$etime = strtotime($this->_post("endtime"));
			$param = array(
				"arr"   => array("begintime"=>$btime,"endtime"=>$etime),
				"cache" => true,
			);
			parent::do_edit("Groupou",$param);
		}else{
			
			$groupou = D("GroupouView");
			$id      = $this->_param('id','intval',0);
			if(!$id) $this->error(L('cfg_form_tipmsg_illgel'));
			
			if(!$data['begintime']){
				$data['begintime'] = time();
			}
			if(!$data['endtime']){
				$data['endtime'] = time()+60*60*24*7;
			}
			
			$data  = $groupou->find($id);
			$this->assign("d",$data); 
			$this->display();
		}
	}
	
}
?>